#include "minicon.h"

int echo( WCHAR **args, int na, bool escape, bool newlin )
{
   WCHAR *p1, *pw;
   int i, cc;
   
   for ( i = 0; i < na; ++i )
   {
      if ( i > 0 )
         Con::o(' ');

      if ( escape )
      {
         pw = p1 = args[i];
         while ( *pw )
         {
            while ( *p1 && L'\\' != *p1 ) ++p1;
            if ( p1 > pw )
            {
               Con::o(pw, (int)(size_t)(p1 - pw));
               if ( !*p1 )
                  break;
            }
            switch ( p1[1] )
            {
               case L'0':
                  cc = 0; p1 += 2;
                  if ( *p1 >= L'0' && *p1 <= L'7' ) {
                     cc = *p1 - L'0'; ++p1;
                     if ( *p1 >= L'0' && *p1 <= L'7' ) {
                        cc = 8*cc + *p1 - L'0'; ++p1;
                        if ( *p1 >= L'0' && *p1 <= L'7' ) {
                           cc = 8*cc + *p1 - L'0'; ++p1;
                        }
                     }
                  }
                  Con::o((char)cc);
               break;

               case L'\\': Con::o('\\'); p1 += 2; break;
               case L'a':  Con::o('\a'); p1 += 2; break;
               case L'b':  Con::o('\b'); p1 += 2; break;
               case L'f':  Con::o('\f'); p1 += 2; break;
               case L'n':  Con::o('\n'); p1 += 2; break;
               case L'r':  Con::o('\r'); p1 += 2; break;
               case L't':  Con::o('\t'); p1 += 2; break;
               case L'v':  Con::o('\v'); p1 += 2; break;

               case L'c':  newlin=false; p1 += 2; break;

               case L'P':  Con::o('%');  p1 += 2; break; //!!! MY !!!
               case L'Q':  Con::o('"');  p1 += 2; break; //!!!!!!!!!!
               case L'O':  Con::o("'");  p1 += 2; break; // extension
               case L'A':  Con::o('&');  p1 += 2; break; // to
               case L'L':  Con::o('<');  p1 += 2; break; // play
               case L'G':  Con::o('>');  p1 += 2; break; // with
               case L'I':  Con::o('|');  p1 += 2; break; // windows
               case L'S':  Con::o(' ');  p1 += 2; break; // shell
               case L'U':  Con::o('^');  p1 += 2; break; //!!!!!!!!!!
               case L'E':  Con::o('=');  p1 += 2; break; //!!!!!!!!!!
               
               default:    Con::o('\\'); p1 += 1; break;
            }
            pw = p1;
         }
      }
      else
      {
         Con::o( args[i] );
      }
   }

   if ( newlin ) Con::n();
   return 0;
}
